<template>
  <div class="m-25 mt-65">
    <div class="normal-box-mod">
      <div class="normal-box-hd clearfix">
        <div class="normal-box-title">
          <div class="decorate-square"></div>
          <h2>论坛管理</h2>
        </div>
      </div>
      <div class="normal-box-bd">
           <div class="normal-list-page-mod">
          <div class="mb-2">
            <Button-group>
               <Button
                custom-icon="iconfont icon-shanchusekuai"
                size="small"
                type="error"
                @click="batchDel"
              >
                删除
              </Button>
               </Button-group>
          </div>
          <div class="normal-list-page-bd">
            <Table
              border
              :loading="list.loading"
              :columns="list.columns"
              :data="list.tableData"
              @on-selection-change="selectionChange"
            ></Table>
          </div>
          <div class="normal-list-page-ft clearfix">
            <Page
              class="fr"
              :total="queryData.totalRecord"
              :page-size="queryData.pageSize"
              :current="queryData.page"
              @on-change="changePage"
              @on-page-size-change="onPageSizeChange"
              show-elevator
              show-total
              show-sizer
            ></Page>
          </div>
        </div>
        <Modal v-model="modal.mallAdvertising.bigPicVisible" width="1000px">
          <img
            :src="modal.mallAdvertising.bigPicUrl"
            style="width: 50%; height: 50%; display: block; margin: 0 auto"
          />
          <div slot="footer" />
        </Modal>
        <!--新建&修改-->
        <Modal
          v-model="modal.mallAdvertising.isShow"
          :loading="modal.mallAdvertising.loading"
          :mask-closable="false"
          :title="modal.mallAdvertising.title"
          @on-ok="postData"
        >
          <Form
            ref="systemFrom"
            :model="modal.mallAdvertising.formData"
            :rules="modal.mallAdvertising.rules"
            :label-width="120"
          >
            <FormItem label="标题" prop="title">
              <Input
                placeholder="请输入标题"
                v-model="modal.mallAdvertising.formData.title"
                style="width: 300px"
              />
            </FormItem>
            <FormItem label="内容" prop="content">
              <Input
                placeholder="请输入内容"
                v-model="modal.mallAdvertising.formData.content"
                style="width: 300px"
              />
            </FormItem>

            <FormItem label="上传图片" prop="url">
              <upload-image
                v-if="modal.mallAdvertising.isShow"
                ref="upload"
                :uploadList.sync="modal.mallAdvertising.formData.uploadList"
                @imageupload="calcImg"
                imgSize="1"
              ></upload-image>
            </FormItem>
          </Form>
        </Modal>
      </div>
    </div>
  </div>
</template>
<script>
import BBSApi from '@/api/zhcg/bbs.js'
import { Message } from 'iview'
import Uploadoss from '@/components/uploadoss'
import uploadImage from '@/components/upload-image/upload-image'
export default {
  components: {
    uploadImage,
    Uploadoss
  },
  name: 'user',
  data () {
    return {
      // 列表
      modal: {
        positionList: [
          { id: 1, name: '积分商城' },
          { id: 2, name: '新闻快讯' },
          { id: 3, name: '政策制度' },
          { id: 4, name: '分类知识' },
          { id: 5, name: '最新资讯' }
        ],
        mallAdvertising: {
          isShow: false,
          bigPicVisible: false,
          loading: true,
          bigPicUrl: '',
          type: 'create', // create 新建，update 修改
          title: '新建',
          rules: {
            title: [
              {
                required: true,
                message: '请填写标题',
                trigger: 'blur'
              }
            ],
            content: [
              {
                required: true,
                message: '请填写内容',
                trigger: 'blur'
              }
            ]
          },
          formData: {}
        }
      },
      bigPicVisible: false,
      bigPicUrl: '',
      list: {
        loading: false,
        selections: [],
        // 列表展示数据
        tableData: [],
        // 表格表头
        columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          /** 开始自动生成 */
          { title: 'id', key: 'pid', align: 'center', width: '80px' },
          { title: '作者id', key: 'authorid', align: 'center', width: '80px' },
          { title: '作者', key: 'author', align: 'center', width: '100px' },
          {
            title: '作者头像',
            key: 'authorImg',
            align: 'center',
            width: 150,
            render: (h, params) => {
              return h('img', {
                attrs: {
                  src: params.row.authorImg
                },
                style: {
                  width: '100px'
                  //   height: "35px",
                },
                on: {
                  click: () => {
                    this.showBigPic(params.row.authorImg) // 此处再用一个方法，建一个modal把图放大。
                  }
                }
              })
            }
          },
          { title: '发帖内容', key: 'message', align: 'center' },
          { title: '评论数量', key: 'commentNum', align: 'center', width: '100px' },
          { title: '点赞数量', key: 'browseNum', align: 'center', width: '100px' },
          {
            title: '发帖时间',
            key: 'createTime',
            align: 'center',
            width: '150px'
          }
          /** 自动生成结束 */
          //   {
          //     title: '操作',
          //     align: 'center',
          //     minWidth: 100,
          //     fixed: 'right',
          //     render: (h, params) => {
          //       return h('div', { class: 'action-group' }, [
          //         h(
          //           'Tooltip',
          //           {
          //             props: {
          //               placement: 'right',
          //               content: '删除'
          //             },
          //             style: {
          //               cursor: 'pointer'
          //             }
          //           },
          //           [
          //             h(
          //               'Button',
          //               {
          //                 props: {
          //                   type: 'error',
          //                   size: 'small'
          //                 },
          //                 style: {
          //                   marginRight: '5px',
          //                   padding: '1px 2px'
          //                 },
          //                 on: {
          //                   click: () => {
          //                     this.delData([params.row.id])
          //                   }
          //                 }
          //               },
          //               [
          //                 h('Icon', {
          //                   props: {
          //                     type: ' iconfont icon-shanchusekuai',
          //                     size: 18
          //                   }
          //                 })
          //               ]
          //             )
          //           ]
          //         )
          //       ])
          //     }
          //   }
        ]
      },
      // 页面查询数据
      queryData: {
        limit: 10,
        page: 1,
        totalRecord: 0
      }
    }
  },
  methods: {
    // 图片的校验
    calcImg () {
      let imgUrl = this.modal.mallAdvertising.formData.uploadList.map((v) => {
        if (v.length < 1) return v
        v = v.response.res.requestUrls[0]
        return v
      })
      this.modal.mallAdvertising.formData.imgUrl = imgUrl[0]
      console.log(this.modal.mallAdvertising.formData.imgUrl)
    },
    // 显示新建弹窗
    showCreateModal () {
      this.modal.mallAdvertising.type = 'create'
      this.modal.mallAdvertising.title = '新建'
      this.modal.mallAdvertising.formData = {}
      this.modal.mallAdvertising.isShow = true
    },

    showBigPic (item) {
      this.modal.mallAdvertising.bigPicVisible = true
      this.modal.mallAdvertising.bigPicUrl = item
    },
    // 获取列表
    getTableData () {
      const that = this
      that.queryData.totalRecord = undefined
      BBSApi
        .getPageList(that.queryData)
        .then((res) => {
          that.list.tableData = res.data.list
          that.queryData.totalRecord = res.data.totalRecord
          that.queryData.page = res.data.pageNum
          that.queryData.limit = res.data.pageSize
        })
        .catch((error) => {
          Message.error(error)
        })
    },
    postData () {
      const that = this
      that.$refs['systemFrom'].validate((valid) => {
        if (valid) {
          if (!that.modal.mallAdvertising.formData.id) {
            // 新建
            notifyApi
              .add(that.modal.mallAdvertising.formData)
              .then((res) => {
                that.modal.mallAdvertising.loading = false
                if (!res.code) {
                  that.modal.mallAdvertising.isShow = false
                  that.getTableData()
                  that.$Notice.success({
                    title: '保存成功!'
                  })
                } else {
                  that.modal.mallAdvertising.isShow = true
                  that.$Notice.error({
                    title: res.message
                  })
                }
              })
              .catch((error) => {
                Message.error(error)
              })
          } else {
            // 修改
            that.modal.mallAdvertising.formData.startTime = global.time(
              that.modal.mallAdvertising.formData.startTime
            )
            that.modal.mallAdvertising.formData.endTime = global.time(
              that.modal.mallAdvertising.formData.endTime
            )
            MallAdvertisingApi.update(
              that.modal.mallAdvertising.formData.id,
              that.modal.mallAdvertising.formData
            )
              .then((res) => {
                that.modal.mallAdvertising.loading = false
                console.log(that.modal.mallAdvertising.formData)
                if (!res.code) {
                  that.modal.mallAdvertising.isShow = false
                  that.getTableData()
                  that.$Notice.success({
                    title: '保存成功!'
                  })
                } else {
                  that.modal.mallAdvertising.isShow = true
                  that.$Notice.error({
                    title: res.message
                  })
                }
              })
              .catch((error) => {
                Message.error(error)
              })
          }
        } else {
          that.modal.mallAdvertising.loading = false
          that.$nextTick(() => {
            that.modal.mallAdvertising.loading = true
          })
        }
      })
    },

    // 删除
    delData (ids) {
      if (!ids.length) {
        return
      }
      this.$Modal.confirm({
        title: '确认删除',
        content:
          '<p>您确认删除id为 <strong>' +
          ids.toString() +
          '</strong> 的记录吗?</p><p>删除后将无法撤销，请谨慎操作！</p>',
        loading: true,
        onOk: () => {
          BBSApi.del({
            ids: ids
          }).then((res) => {
            this.$Modal.remove()
            if (!res.code) {
              this.$Notice.success({
                title: '删除成功'
              })
              this.queryData.page = 1
              this.getTableData()
            } else {
              this.$Notice.error({
                title: res.message
              })
            }
          })
        }
      })
    },

    // 排量删除
    batchDel () {
      let ids = []
      this.list.selections.forEach((element) => {
        ids.push(element.pid)
      })
      if (!ids.length) {
        this.$Message.error({
          content: '请先选择要删除的项！',
          duration: 3
        })
      }
      //   console.log('ids', this.list.selections)
      this.delData(ids)
    },
    // 查询
    inQuire () {
      const that = this
      that.getTableData()
    },
    // 全选，不全选
    selectionChange (args) {
      this.list.selections = args
    },

    // 每页显示记录数变更
    onPageSizeChange (pageSize) {
      this.queryData.limit = pageSize
      this.getTableData()
    },

    // 换页
    changePage (page) {
      this.queryData.page = page
      this.getTableData()
    }
  },

  created () {
    this.getTableData()
  }
}
</script>
